package com.wa.graphql.schemas;
import com.wa.beans.Item;
import com.wa.beans.ItemList;
import com.wa.pojo.BaseResponse;
import com.wa.pojo.GraphqlPojo;
import com.wa.service.impl.ItemServiceImpl;
import com.wa.util.GraphSchemaBeanUtils;
import graphql.Scalars;
import graphql.schema.*;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

/**
 * @author gary
 * @date 2021年11月23日 9:55
 */
@Component
public class TestSchema {

    @Resource
    ItemServiceImpl itemService;

    private GraphQLOutputType itemOutputType;

    private GraphQLOutputType itemListOutputType;

    @PostConstruct
    public void initSchema() {
        itemOutputType = GraphSchemaBeanUtils.beanSchema(Item.class, newObject().name("queryIdBuild")).build();
        GraphQLObjectType.Builder itemListBuild = newObject().name("queryItemListPojo");
        //反射自动定义字段
        GraphSchemaBeanUtils.beanSchema(ItemList.class,itemListBuild);
        itemListBuild.field(GraphQLFieldDefinition.newFieldDefinition().name("itemList").type(new GraphQLList(itemOutputType)));
        itemListOutputType = itemListBuild.build();
    }

    public GraphQLOutputType getItemOutputType() {
        return itemOutputType;
    }

    public GraphQLOutputType getItemListOutputType() {
        return itemListOutputType;
    }

    /**
     * @param outputType
     * @return
     */
    public GraphQLFieldDefinition queryById(GraphQLOutputType outputType) {
        //定义的queryById就是query定义的名称 "query":"{queryById(id:1){data{id,name,code,createTime,searchDate}}}"
        return newFieldDefinition().name("queryById")
                //定义参数queryById(id:1)
                .argument(GraphQLArgument.newArgument().name("id").type(Scalars.GraphQLLong).build())
                .type(outputType)
                .dataFetcher(queryByIdDataFetcher()).build();
    }

    private DataFetcher<?> queryByIdDataFetcher() {
        return environment -> {
            Long id = environment.getArgument("id");
            if (id == null) {
                throw new RuntimeException("搜索关键词不能为空");
            }
            Item item = itemService.queryById(id);
            GraphqlPojo<Item> graphqlPojo = new GraphqlPojo<>();
            graphqlPojo.setCode(BaseResponse.SUCCESS_RESULT.getResponseCode());
            graphqlPojo.setData(item);
            return graphqlPojo;
        };
    }

    /**
     * @param outputType
     * @return
     */
    public GraphQLFieldDefinition queryItemList(GraphQLOutputType outputType) {
        return newFieldDefinition().name("queryItemList")
                .type(outputType)
                .dataFetcher(queryItemListDataFetcher()).build();
    }

    private DataFetcher<?> queryItemListDataFetcher() {
        return environment -> {
            ItemList itemList = itemService.queryItemList();
            GraphqlPojo<ItemList> graphqlPojo = new GraphqlPojo<>();
            graphqlPojo.setCode(BaseResponse.SUCCESS_RESULT.getResponseCode());
            graphqlPojo.setData(itemList);
            return graphqlPojo;
        };
    }
}
